package com.yss.reportworld.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.exception.BusinessException;
import com.yss.reportworld.domain.arrwrap.TcRepDataset;
import com.yss.reportworld.domain.arrwrap.TpRepBasicInfo;
import com.yss.reportworld.mapper.arrwrap.TcRepDatasetMapper;
import com.yss.reportworld.mapper.arrwrap.TpRepBasicInfoMapper;
import com.yss.reportworld.util.TableInfoUtil;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.reportworld.service.ITcRepParaService;
import com.yss.reportworld.mapper.arrwrap.TcRepParaMapper;
import com.yss.reportworld.domain.arrwrap.TcRepPara;

/**
 * 报表基本信息表Service业务层处理
 *
 * @author yss
 * @date 2023-07-14
 */
@Slf4j
@Service
public class TcRepParaServiceImpl extends ServiceImpl<TcRepParaMapper, TcRepPara> implements ITcRepParaService {
    @Autowired
    private TcRepParaMapper tcRepParaMapper;
    @Autowired
    private TpRepBasicInfoMapper tpRepBasicInfoMapper;
    @Autowired
    private TcRepDatasetMapper tcRepDatasetMapper;

    /**
     * 查询报表基本信息表列表
     *
     * @param tcRepPara 报表基本信息表
     * @return 报表基本信息表
     */
    @Override
    public List<TcRepPara> selectTcRepParaList(TcRepPara tcRepPara) {
        try {
            QueryWrapper<TcRepPara> wrapper = new QueryWrapper();
            wrapper.orderByAsc("REPORT_ID");
            if (StringUtils.isNull(tcRepPara) || StringUtils.isEmpty(tcRepPara.getRegulationRule())) {
                return new ArrayList<>();
            }
            if (StringUtils.isNotEmpty(tcRepPara.getRegulationRule())) {
                wrapper.eq("REGULATION_RULE", tcRepPara.getRegulationRule());
            }
            if (StringUtils.isNotEmpty(tcRepPara.getReportName())) {
                wrapper.like("REPORT_NAME", tcRepPara.getReportName());
            }
            TableInfoUtil.handleTableVersion(tcRepPara.getHisVersion());
            List<TcRepPara> tcRepParaList = tcRepParaMapper.selectList(wrapper);
            if (StringUtils.isEmpty(tcRepParaList)) {
                return tcRepParaList;
            }
            // tcRepPara里得 ReportTablename有拼接逗号得
            // 获取对应的 report_name
            List<String> listReportTablename = this.getListReportTablename(tcRepParaList);
            // 报表编码，查询结果集的时候使用
            Set<String> listReportId = tcRepParaList.stream()
                    .map(e -> new String(e.getReportId()))
                    .collect(Collectors.toSet());
            if (StringUtils.isEmpty(listReportId)) {
                return new ArrayList<>();
            }
            // 先通过 基本信息 report_tablename 匹配取出 TP_REP_BASIC_INFO
            //报表基础信息
            QueryWrapper<TpRepBasicInfo> tpRepBasicInfoQueryWrapper = new QueryWrapper<>();
            tpRepBasicInfoQueryWrapper.in("REPORT_OVER_NAME", listReportTablename);
            tpRepBasicInfoQueryWrapper.eq("RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<TpRepBasicInfo> tpRepBasicInfoList = tpRepBasicInfoMapper.selectList(tpRepBasicInfoQueryWrapper);
            if (StringUtils.isEmpty(tpRepBasicInfoList)) {
                return new ArrayList<>();
            }
            QueryWrapper<TcRepDataset> tcRepDatasetQueryWrapper = new QueryWrapper<>();
            tcRepDatasetQueryWrapper.in("REPORT_ID", listReportId);
            tcRepDatasetQueryWrapper.eq("RW_DB_DATABASE_TYPE", tcRepPara.getDataBaseType());
            tcRepDatasetQueryWrapper.orderByAsc("REPORT_ID");
            tcRepDatasetQueryWrapper.eq("RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<TcRepDataset> tcRepDatasetList = tcRepDatasetMapper.selectList(tcRepDatasetQueryWrapper);
            if (StringUtils.isEmpty(tcRepDatasetList)) {
                return new ArrayList<>();
            }
            return tcRepParaList;
        } catch (Exception ex) {
            throw new BusinessException("获取报表基本信息列表失败，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 获取用要的报表落地表表名信息
     *
     * @param tcRepParaList
     * @return
     */
    private List<String> getListReportTablename(List<TcRepPara> tcRepParaList) {
        List<String> listReportTablename = new ArrayList<>();
        for (TcRepPara repPara : tcRepParaList) {
            if (StringUtils.isNull(repPara) || StringUtils.isEmpty(repPara.getReportTablename())) {
                continue;
            }
            if (repPara.getReportTablename().indexOf(",") > -1) {
                String[] arr = repPara.getReportTablename().split(",");
                if (null == arr || arr.length <= 0) {
                    continue;
                }
                listReportTablename.addAll(Arrays.asList(arr));
            }
            listReportTablename.add(repPara.getReportTablename());
        }
        if (StringUtils.isEmpty(listReportTablename)) {
            throw new BusinessException("报表基本信息表中没有获取导对应的落地表名的信息，请检查！");
        }
        return listReportTablename;
    }

}
